//
//  TKTableHeaderView.m
//  TableGridDemo
//
//  Created by 吳瀾洲 on 2020/8/21.
//  Copyright © 2020 kingboyrang. All rights reserved.
//

#import "TKTableTDView.h"

#define KdefaultHeaderBaseTag  300

@interface TKTableTDView ()

@end

@implementation TKTableTDView


- (instancetype)initWithFrame:(CGRect)frame
{
    if ((self = [super initWithFrame:frame]))
    {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if ((self = [super initWithCoder:aDecoder]))
    {
        [self setUp];
    }
    return self;
}

- (instancetype)initWithTitiles:(NSArray<NSString *> *)titles frame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        
        [self setUp];
        self.titles = titles;
        //[self createHeader];
        
    }
    return self;
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}

#pragma mark - 属性
- (void)setLineColor:(UIColor *)lineColor{
    if (_lineColor!=lineColor) {
        _lineColor = lineColor;
        [self setNeedsDisplay];
    }
}
- (void)setTextColor:(UIColor *)textColor{
    if (_textColor!=textColor) {
        _textColor = textColor;
        if (self.titles&&[self.titles count]>0) {
            for (NSInteger i=0;i<[self.titles count];i++) {
                UILabel *label = (UILabel *)[self viewWithTag:KdefaultHeaderBaseTag + i];
                if (label) {
                    label.textColor = textColor;
                }
            }
        }
    }
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment{
    if (_textAlignment != textAlignment) {
        _textAlignment = textAlignment;
        if (self.titles&&[self.titles count]>0) {
            for (NSInteger i=0;i<[self.titles count];i++) {
                UILabel *label = (UILabel *)[self viewWithTag:KdefaultHeaderBaseTag + i];
                if (label) {
                    label.textAlignment = textAlignment;
                }
            }
        }
    }
}

- (void)setTextFont:(UIFont *)textFont{
    if (_textFont != textFont) {
        _textFont = textFont;
        if (self.titles&&[self.titles count]>0) {
            for (NSInteger i=0;i<[self.titles count];i++) {
                UILabel *label = (UILabel *)[self viewWithTag:KdefaultHeaderBaseTag + i];
                if (label) {
                    label.font = textFont;
                }
            }
        }
    }
}

- (void)setTitles:(NSArray<NSString *> *)titles{
    if (_titles!=titles) {
        
        if (_titles && [_titles count]>0) {
            //移除旧的
            for (NSInteger i=0; i<[_titles count]; i++) {
                UIView *subview = [self viewWithTag:KdefaultHeaderBaseTag + i];
                if (subview) {
                     [subview removeFromSuperview];
                }
            }
        }
        _titles = titles;
        [self createHeader:titles];
    }
}
- (void)setDirection:(LineDirectionType)direction{
    if (_direction != direction) {
        _direction = direction;
        [self setNeedsDisplay];
    }
}
- (void)setShowMiddleLineBox:(BOOL)showMiddleLineBox{
    if (_showMiddleLineBox != showMiddleLineBox) {
        _showMiddleLineBox = showMiddleLineBox;
        [self setNeedsDisplay];
    }
}
#pragma mark 监听横竖屏方向改变
- (void)statusBarOrientationChange:(NSNotification *)notification
{
    [self setNeedsDisplay];
    NSLog(@"屏幕发生旋转");
}
#pragma mark - 私有方法
- (void)setUp{
    
   self.backgroundColor = [UIColor colorWithRed:230/255.0 green:233/255.0 blue:239/255.0 alpha:1.0];
   self.textFont = [UIFont systemFontOfSize:15];
   self.textAlignment = NSTextAlignmentCenter;
   self.lineColor = [UIColor lightGrayColor];
   self.direction = LineDirectionTypeAll;
   self.showMiddleLineBox = YES;
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    
}
- (void)createHeader:(NSArray <NSString *> *)titles{
    
    if (titles&&[titles count]>0) {
          
        CGFloat width = self.frame.size.width/[titles count] * 1.0;
        
        for (NSInteger i=0;i<[titles count];i++) {
            CGRect r = CGRectMake(i*width, 0, width, self.frame.size.height);
           
            UILabel *label = [[UILabel alloc] initWithFrame:r];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = self.textAlignment;
            label.text = [titles objectAtIndex:i];
            label.font = self.textFont;
            label.textColor = self.textColor;
            label.tag = KdefaultHeaderBaseTag+i;
            if (self.TKLabelCotrolBlock) {
                UIView *view = self.TKLabelCotrolBlock(label,[titles objectAtIndex:i],r);
                if (view) {
                    [self addSubview:view];
                }else{
                    [self addSubview:label];
                }
                
            }else{
                [self addSubview:label];
            }
            
        }
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //NSLog(@"1");
    //旋转时，不会调用drawRect
    if (self.titles&&[self.titles count]>0) {
        CGFloat width = self.frame.size.width/[self.titles count];
        for (NSInteger i=0;i<[self.titles count];i++) {
            UIView *label = [self viewWithTag:KdefaultHeaderBaseTag + i];
            if (label) {
                CGRect r = label.frame;
                r.size.width = width;
                r.origin.x = i * width;
                label.frame = r;
                
            }
        }
    }
}

- (void)drawRect:(CGRect)rect{
    
    //NSLog(@"2");
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    //线条的宽度
    CGContextSetLineWidth(context, 0.5);
    //设置线条的颜色
    CGContextSetStrokeColorWithColor(context,self.lineColor.CGColor);
    if (self.direction & LineDirectionTypeNone) {
        
    }else if(self.direction & LineDirectionTypeAll){
        //1.画顶部
        CGContextMoveToPoint(context, 0, 0); //起点坐标
        CGContextAddLineToPoint(context, self.frame.size.width, 0); //终点坐标
        
        //2.画中间
        //2.1 中间最左边
        CGContextMoveToPoint(context, 0, 0);
        CGContextAddLineToPoint(context, 0, self.frame.size.height);
        //2.2 中间最右边
        CGContextMoveToPoint(context, self.frame.size.width, 0);
        CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
        
        if (self.titles&&[self.titles count]>0 && self.showMiddleLineBox) {
            
            CGFloat width = self.frame.size.width/[self.titles count];
            for (NSInteger i=1; i<[self.titles count]; i++) {
                
                CGContextMoveToPoint(context, width * i, 0);
                CGContextAddLineToPoint(context, width * i, self.frame.size.height);
            }
            
        }
        //3.画底部
        CGContextMoveToPoint(context, 0, self.frame.size.height);
        CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
        
    }else if ((self.direction & LineDirectionTypeLeft)
              || (self.direction & LineDirectionTypeRight)
              || (self.direction & LineDirectionTypeBottom)
              || (self.direction & LineDirectionTypeTop)){
        
        // 中间最左边
        if (self.direction & LineDirectionTypeLeft) {
            CGContextMoveToPoint(context, 0, 0);
            CGContextAddLineToPoint(context, 0, self.frame.size.height);
        }
        // 中间最右边
        if (self.direction & LineDirectionTypeRight) {
            CGContextMoveToPoint(context, self.frame.size.width, 0);
            CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
        }
        //顶部
        if (self.direction & LineDirectionTypeTop) {
            CGContextMoveToPoint(context, 0, 0); //起点坐标
            CGContextAddLineToPoint(context, self.frame.size.width, 0); //终点坐标
        }
        //底部
        if (self.direction & LineDirectionTypeBottom) {
            CGContextMoveToPoint(context, 0, self.frame.size.height);
            CGContextAddLineToPoint(context, self.frame.size.width, self.frame.size.height);
        }
        if (self.titles&&[self.titles count]>0 && self.showMiddleLineBox) {
            
            CGFloat width = self.frame.size.width/[self.titles count];
            for (NSInteger i=1; i<[self.titles count]; i++) {
                
                CGContextMoveToPoint(context, width * i, 0);
                CGContextAddLineToPoint(context, width * i, self.frame.size.height);
            }
            
        }
        
        
    }
    //绘制路径
    CGContextStrokePath(context);
    
}

@end
